***************************************************************
* Author: Jules Gazeaud
* Date: August 15, 2022
* Description: This do-file creates tables 1-3 in the published 
* paper and tables A2-A11 in the online appendix.
***************************************************************


* TABLES 1-3
* ------------

	** Women sample
	
	use "${data}/data_women.dta" , clear
		
	
		* Preparation

		do "${data}/Lasso_var.do"		
		local i = 1
		gen pvalue_t0 = .
		gen pvalue_t1 = .
		gen pvalue_t2 = .
		gen pvalue_t3 = .
		gen varname = ""

			
		** Outcomes with no baseline value for the outcomes (not ANCOVA)
	
		foreach var in 	iga_precovid c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga c1_hengag_agr chemicals  ///
						value19_n_iga z_financial_index  z_agency_index c1_cropprodna c1_cropvalna ///
						d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 ///
						z_assets19_total migration_future_d hh_migrated {
	
				noi di "`var'"
				replace varname = "`var'" if _n == `i'
				sum `var' if trt_cash == 0
				local sd = `r(sd)'
				insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
				insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
				
				** Equation 1
				local temp = "`var'"
				if "`temp'" != "hh_migrated" {
				pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
				}
				if "`temp'" == "hh_migrated" {
				pdslasso `var' trt_cash ($balance $X_poly_and_int) , robust				
				}
				mat A = r(table)
				local p = A[4,1]
				replace pvalue_t0 = `p' if _n == `i'
				store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t) all
			
		
				** Equation 2
				if "`temp'" != "hh_migrated" {
				pdslasso `var' trt_cash_0 trt_cash_1 ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
				}
				if "`temp'" == "hh_migrated" {
				pdslasso `var' trt_cash_0 trt_cash_1 ($balance $X_poly_and_int) , robust
				}
				mat A = r(table)
				local p0 = A[4,1]
				replace pvalue_t1 = `p0' if _n == `i'
				local p1 = A[4,2]
				replace pvalue_t2 = `p1' if _n == `i'
				store_est_tpl using "sample_table.csv" , coef(trt_cash_0) name(`var'_t0) all
				store_est_tpl using "sample_table.csv" , coef(trt_cash_1) name(`var'_t1) all				
				local n = `e(N)'
				insert_into_file using "sample_table.csv", key(`var'_t1_n) value(" `n'") format(%-9.0fc)
				test trt_cash_0 = trt_cash_1
				replace pvalue_t3 = `r(p)' if _n == `i'
				local ++i

				insert_into_file using "sample_table.csv", key(`var'_test) value(" `r(p)'") format(%6.3f)
		
		}
		
				
		** Outcomes with baseline value for the outcomes (ANCOVA)
	
		
		foreach var in 		c1_job_iga business_profit business_employee total_employement c1_hoursprimjob ///
							value_n_iga does_livestock conso_tot_pc conso_food_pc conso_nofood_pc ///
							z_assets_total b4_econshock b4_othershock b4_extreme b4_normal migration_d {
				
				noi di "`var'"
				replace varname = "`var'" if _n == `i'
				sum `var' if trt_cash == 0
				local sd = `r(sd)'
				insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
				insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
				
				* Equation 1
				pdslasso `var' trt_cash ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
				mat A = r(table)
				local p = A[4,1]
				replace pvalue_t0 = `p' if _n == `i'
				store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t) all

				
				* Equation 2
				pdslasso `var' trt_cash_0 trt_cash_1 ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
				mat A = r(table)
				local p0 = A[4,1]
				replace pvalue_t1 = `p0' if _n == `i'
				local p1 = A[4,2]
				replace pvalue_t2 = `p1' if _n == `i'
				store_est_tpl using "sample_table.csv" , coef(trt_cash_0) name(`var'_t0) all
				store_est_tpl using "sample_table.csv" , coef(trt_cash_1) name(`var'_t1) all
				local n = `e(N)'
				insert_into_file using "sample_table.csv", key(`var'_t1_n) value(" `n'") format(%-9.0fc)
				test trt_cash_0 = trt_cash_1
				replace pvalue_t3 = `r(p)' if _n == `i'
				local ++i
				insert_into_file using "sample_table.csv", key(`var'_test) value(" `r(p)'") format(%6.3f)

		}
		
		
	
		** Partner sample	
		preserve
		use "${data}/data_partners.dta" , clear
		do "${data}/Lasso_var.do"
		foreach var in z_male_index {
		sum `var' if trt_cash_part_1==0
		local sd = `r(sd)'
		insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
		pdslasso `var' trt_cash_part_1 ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
		mat A = r(table)
		local p_dialogue = A[4,1]
		di `p_dialogue'
		store_est_tpl using "sample_table.csv" , coef(trt_cash_part_1) name(`var'_t1) all
		}
		restore
	
		
		** FDR q-values
		drop if pvalue_t0==. 
		keep pvalue* varname	
		reshape long pvalue_t , i(varname) j(t)
		set obs `=_N+1'
		replace varname = "z_male_index" if _n == 145
		replace pvalue_t = `p_dialogue' if _n == 145
		replace t = 2 if _n == 145
		rename pvalue_t pval
		do "${data}/fdr_sharpened_qvalues.do"
		foreach var in c1_job_iga iga_precovid business_profit business_employee total_employement c1_hoursprimjob c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga /// 
						c1_hengag_agr chemicals c1_cropprodna c1_cropvalna does_livestock value19_n_iga value_n_iga /// 
						z_financial_index z_agency_index d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 /// 
						conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total z_assets19_total migration_d hh_migrated migration_future_d b4_econshock b4_othershock b4_extreme b4_normal z_male_index {
		sum bky06_qval if varname == "`var'" & t == 0
		insert_into_file using sample_table.csv, key(`var'_qpvalue_t) value(" `r(mean)'") format(%6.3f)
		sum bky06_qval if varname == "`var'" & t == 1
		insert_into_file using sample_table.csv, key(`var'_qpvalue_t0) value(" `r(mean)'") format(%6.3f)
		sum bky06_qval if varname == "`var'" & t == 2
		insert_into_file using sample_table.csv, key(`var'_qpvalue_t1) value(" `r(mean)'") format(%6.3f)
		sum bky06_qval if varname == "`var'" & t == 3
		insert_into_file using sample_table.csv, key(`var'_qpvalue_test) value(" `r(mean)'") format(%6.3f) 
		}

		
	table_from_tpl, t("TPL_tab1.tex") r("sample_table.csv") o("tab1.tex")
	table_from_tpl, t("TPL_tab2.tex") r("sample_table.csv") o("tab2.tex")
	table_from_tpl, t("TPL_tab3.tex") r("sample_table.csv") o("tab3.tex")
		
		

******************************************
*	       Online appendix	 		     *
******************************************

do "${tables}/stata-tex.do"

* FIGURE A4
* ----------			
				
	use "${data}/data_women.dta" , clear
			
		global plotregion 		plotregion(lcolor(black))
		global graphregion 		graphregion(color(white) margin(5 5 5 5))
		global legendregion 	region(lstyle(none) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white)) size(medsmall) colgap(huge) keygap(vsmall) just(center) symxsize(huge))
		global laboptions		labsize(4) nogextend nogrid
		global titleoptions		size(large)
		graph set ps fontface 	"Times New Roman"
			
			* Panel A
			#delimit ;
				twoway 
				(histogram  diff_date if trt_cash == 1 , color(gs2%30) frequency width(1)) , 
				xline(30.5, lcolor(red)) 
				xtitle("Date of interview relative to median date in Imada", $titleoptions) 
				xlabel(-10(10)100 , $laboptions )  
				ytitle("Frequency", $titleoptions)
				xsize(6)
				ylabel(0(100)300, $laboptions)
				legend(off)
				$plotregion $graphregion ;
			#delimit cr	
			graph export "${figures}/figA5a.png", as(png) replace
			
			* Panel B
			replace diff_date = . if diff_date < 5	
			#delimit ;
				twoway 
				(histogram  diff_date if trt_cash == 1 , color(gs2%30) frequency start(0) width(1)), 
				xline(30.5, lcolor(red)) 
				xtitle("Date of interview relative to median date in Imada", $titleoptions) 
				xlabel(5(15)95, $laboptions )  
				ytitle("Frequency", $titleoptions)
				xsize(6)
				ylabel(0(5)15, $laboptions)
				legend(off)
				$plotregion $graphregion ;
			#delimit cr	
			graph export "${figures}/figA5b.png", as(png) replace

		
* TABLE A2 
* ----------

	use "${data}/data_women.dta" , clear
		
		drop if Intervention == "Cash Grants - Partenaire"
		replace trt_cash_1 = . if trt_cash_0 == 1
		replace trt_cash_0 = . if trt_cash_1 == 1
		
		* Means, sd and ttests and normalized diff
		foreach var in $balance {
		forvalues x = 0/2 {
		sum `var' if group == `x'
		local sd = `r(sd)'
		local n = `r(N)'
		insert_into_file using "sample_table.csv", key(`var'_mean`x') value(" `r(mean)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_sd`x') value(" `sd'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_n`x') value(" `n'") format(%-9.0fc)
		ttest `var' , by(trt_cash_`x')
		local n1 = `r(N_1)'
		local n2 = `r(N_2)'
		local ntot = `n1' + `n2'
		insert_into_file using "sample_table.csv", key(`var'_p`x') value(" `r(p)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_ntot`x') value(" `ntot'") format(%-9.0fc)
		stddiff `var' , by(trt_cash_`x')
		matrix b = r(output)
		local ndiff = b[1,5]	
		insert_into_file using "sample_table.csv", key(`var'_ndiff`x') value(" `ndiff'") format(%6.3f)
		}
		}
		
		* Omnibus test
		forvalues x = 0/2 {
		reg trt_cash_`x'	$balance
		test $balance
		insert_into_file using "sample_table.csv", key(omnip`x') value(" `r(p)'") format(%6.3f)
		}
		
	table_from_tpl, t("TPL_tabA2.tex") r("sample_table.csv") o("tabA2.tex")
		



* TABLE A3
* ---------

	use "${data}/data_women.dta" , clear
		
		drop if Intervention == "Cash Grants - Partenaire"
		replace trt_cash_1 = . if trt_cash_0 == 1
		replace trt_cash_0 = . if trt_cash_1 == 1
		
		foreach var in attrition_tot attrition Dead Moved Refusal Other {
		forvalues x = 0/2 {
		sum `var' if group == `x'
		local sd = `r(sd)'
		local n = `r(N)'
		insert_into_file using "sample_table.csv", key(`var'_mean`x') value(" `r(mean)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_sd`x') value(" `sd'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_n`x') value(" `n'") format(%-9.0fc)
		ttest `var' , by(trt_cash_`x')
		local n1 = `r(N_1)'
		local n2 = `r(N_2)'
		local ntot = `n1' + `n2'
		insert_into_file using "sample_table.csv", key(`var'_p`x') value(" `r(p)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'_ntot`x') value(" `ntot'") format(%-9.0fc)
		stddiff `var' , by(trt_cash_`x')
		matrix b = r(output)
		local ndiff = b[1,5]	
		insert_into_file using "sample_table.csv", key(`var'_ndiff`x') value(" `ndiff'") format(%6.3f)
		}
		}
		
	table_from_tpl, t("TPL_tabA3.tex") r("sample_table.csv") o("tabA3.tex")



		
* TABLE A4
* ---------

	use "${data}/data_women.dta" , clear
		
		replace trt_cash_1 = . if trt_cash_0 == 1
		replace trt_cash_0 = . if trt_cash_1 == 1
		drop if Intervention == "Cash Grants - Partenaire" | attrition == 1
		
		* Means, sd and ttests and normalized diff
		foreach var in $balance {
		forvalues x = 0/2 {
		sum `var' if group == `x'
		local sd = `r(sd)'
		local n = `r(N)'
		insert_into_file using "sample_table.csv", key(`var'2_mean`x') value(" `r(mean)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'2_sd`x') value(" `sd'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'2_n`x') value(" `n'") format(%-9.0fc)
		ttest `var' , by(trt_cash_`x')
		local n1 = `r(N_1)'
		local n2 = `r(N_2)'
		local ntot = `n1' + `n2'
		insert_into_file using "sample_table.csv", key(`var'2_p`x') value(" `r(p)'") format(%6.3f)
		insert_into_file using "sample_table.csv", key(`var'2_ntot`x') value(" `ntot'") format(%-9.0fc)
		stddiff `var' , by(trt_cash_`x')
		matrix b = r(output)
		local ndiff = b[1,5]	
		insert_into_file using "sample_table.csv", key(`var'2_ndiff`x') value(" `ndiff'") format(%6.3f)
		}
		}
		
		* Omnibus test
		forvalues x = 0/2 {
		reg trt_cash_`x' 	$balance
		test $balance
		insert_into_file using "sample_table.csv", key(omnip`x'2) value(" `r(p)'") format(%6.3f)
		}
		
	table_from_tpl, t("TPL_tabA4.tex") r("sample_table.csv") o("tabA4.tex")

			
			
	
	
* TABLES A5
* -------------------
									
			*** A. IPW (column 2)
			* ---------------------
			
				use "${data}/data_women.dta" , clear
				drop if Intervention == "Follow up - TCLP"
				do "${data}/Lasso_var.do"	
				
				* Preparation
				global baseline_outcomes c1_job_iga_bl does_livestock_bl business_profit_bl business_employee_bl total_employement_bl conso_tot_pc_bl conso_food_pc_bl conso_nofood_pc_bl z_assets_total_bl value_n_bl value_n_iga_bl
				reg responded $balance $X_poly_and_int $baseline_outcomes , ro
				di e(r2_a)
				predict pscore
				bysort responded: sum pscore
				gen weight = 1/(1-pscore) if responded == 0
				replace weight = 1/pscore if responded == 1
				replace weight = . if attrition == 1
				
				* Analysis not ANCOVA
						foreach var in migration_future_d iga_precovid c1_hengag_agr chemicals z_financial_index  z_agency_index c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga c1_cropprodna c1_cropvalna value19_n_iga d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 z_assets19_total {
						pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) [pweight=weight], partial(i.a1_enumerator tclp) robust
						store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_ipw) all
						local n = `e(N)'
						insert_into_file using "sample_table.csv", key(`var'_t_ipw_n) value(" `n'") format(%-9.0fc)
						}
				
				* Analysis ANCOVA
						foreach var in c1_hoursprimjob b4_econshock b4_othershock b4_extreme b4_normal migration_d c1_job_iga does_livestock business_profit business_employee total_employement conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total value_n_iga {
						pdslasso `var' trt_cash ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) [pweight=weight], partial(i.a1_enumerator `var'_bl tclp) robust
						store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_ipw) all
						local n = `e(N)'
						insert_into_file using "sample_table.csv", key(`var'_t_ipw_n) value(" `n'") format(%-9.0fc)
						}		
			
						
			
			*** B. Behaghel (column 3)
			* ------------------

					
				use "${data}/data_women.dta" , clear
				drop if Intervention == "Follow up - TCLP"
				do "${data}/Lasso_var.do"	
				drop if attrition == 1
				count if diff_date >= 30.5 & trt_cash == 1 & diff_date != . // Trim 48 observations => 30.5 days
				
				* Not ANCOVA
					foreach var in migration_future_d iga_precovid c1_hengag_agr chemicals z_financial_index  z_agency_index c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga c1_cropprodna c1_cropvalna value19_n_iga d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 z_assets19_total {
					pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) if (diff_date <= 30.5 & trt_cash == 1) | trt_cash == 0 , partial(i.a1_enumerator tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_behag) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_behag_n) value(" `n'") format(%-9.0fc)
					}
				
				* ANCOVA
					foreach var in c1_hoursprimjob b4_econshock b4_othershock b4_extreme b4_normal migration_d c1_job_iga does_livestock business_profit business_employee total_employement conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total value_n_iga {
					pdslasso `var' trt_cash ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) if (diff_date <= 30.5 & trt_cash == 1) | trt_cash == 0 , partial(i.a1_enumerator `var'_bl tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_behag) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_behag_n) value(" `n'") format(%-9.0fc)
					}		
				
				
					
					
			*** C. Kling and Liebman 0.10 (columns 4-5)
			* ---------------------------------------
				
					use "${data}/data_women.dta" , clear
					do "${data}/Lasso_var.do"	
					drop if Intervention == "Follow up - TCLP"					
					
					* Assign value to enumerator
					replace a1_enumerator = 99 if a1_enumerator == .
					
					* Analysis not ANCOVA
					foreach var in migration_future_d iga_precovid c1_hengag_agr chemicals z_financial_index z_agency_index c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga  c1_cropprodna c1_cropvalna value19_n_iga d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 z_assets19_total {
					gen `var'_lkling = `var'
					gen `var'_ukling = `var'
					sum `var' if trt_cash == 0
					replace `var'_lkling = `r(mean)'+(0.10*`r(sd)') if trt_cash == 0 & attrition == 1		
					replace `var'_ukling = `r(mean)'-(0.10*`r(sd)') if trt_cash == 0 & attrition == 1
					sum `var' if trt_cash == 1
					replace `var'_lkling = `r(mean)'-(0.10*`r(sd)') if trt_cash == 1 & attrition == 1	
					replace `var'_ukling = `r(mean)'+(0.10*`r(sd)') if trt_cash == 1 & attrition == 1	
					pdslasso `var'_lkling trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_lkling10) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_lkling_n) value(" `n'") format(%-9.0fc)
					pdslasso `var'_ukling trt_cash ($balance $X_poly_and_int i.a1_enumerator) , partial(i.a1_enumerator) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_ukling10) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_ukling_n) value(" `n'") format(%-9.0fc)
					}
					
					* Analysis ANCOVA
					foreach var in c1_hoursprimjob b4_econshock b4_othershock b4_extreme b4_normal migration_d c1_job_iga does_livestock business_profit business_employee total_employement conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total value_n_iga {
					gen `var'_lkling = `var'
					gen `var'_ukling = `var'
					sum `var' if trt_cash == 0
					replace `var'_lkling = `r(mean)'+(0.10*`r(sd)') if trt_cash == 0 & attrition == 1		
					replace `var'_ukling = `r(mean)'-(0.10*`r(sd)') if trt_cash == 0 & attrition == 1
					sum `var' if trt_cash == 1
					replace `var'_lkling = `r(mean)'-(0.10*`r(sd)') if trt_cash == 1 & attrition == 1	
					replace `var'_ukling = `r(mean)'+(0.10*`r(sd)') if trt_cash == 1 & attrition == 1	
					pdslasso `var'_lkling trt_cash ($balance $X_poly_and_int i.a1_enumerator `var'_bl tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_lkling10) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_lkling_n) value(" `n'") format(%-9.0fc)
					pdslasso `var'_ukling trt_cash ($balance $X_poly_and_int i.a1_enumerator `var'_bl tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_ukling10) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_ukling_n) value(" `n'") format(%-9.0fc)
					}
				
				
			*** D. Kling and Liebman 0.25 (columns 6-7)
			* ---------------------------------------
				
					use "${data}/data_women.dta" , clear
					do "${data}/Lasso_var.do"	
					drop if Intervention == "Follow up - TCLP"					
					
					* Assign value to enumerator
					replace a1_enumerator = 99 if a1_enumerator == .
					
					* Analysis not ANCOVA
					foreach var in migration_future_d iga_precovid c1_hengag_agr chemicals z_financial_index z_agency_index c1_headjob c1_othermembersworkn c1_headincome30 c1_incomeotheriga  c1_cropprodna c1_cropvalna value19_n_iga d1_ladder_present d1_ladder_1ago d1_ladder3years d1_ladderwealth mhi5 z_assets19_total {
					gen `var'_lkling = `var'
					gen `var'_ukling = `var'
					sum `var' if trt_cash == 0
					replace `var'_lkling = `r(mean)'+(0.25*`r(sd)') if trt_cash == 0 & attrition == 1		
					replace `var'_ukling = `r(mean)'-(0.25*`r(sd)') if trt_cash == 0 & attrition == 1
					sum `var' if trt_cash == 1
					replace `var'_lkling = `r(mean)'-(0.25*`r(sd)') if trt_cash == 1 & attrition == 1	
					replace `var'_ukling = `r(mean)'+(0.25*`r(sd)') if trt_cash == 1 & attrition == 1	
					pdslasso `var'_lkling trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_lkling25) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_lkling_n) value(" `n'") format(%-9.0fc)
					pdslasso `var'_ukling trt_cash ($balance $X_poly_and_int i.a1_enumerator) , partial(i.a1_enumerator) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_ukling25) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_ukling_n) value(" `n'") format(%-9.0fc)
					}
					
					* Analysis ANCOVA
					foreach var in c1_hoursprimjob b4_econshock b4_othershock b4_extreme b4_normal migration_d c1_job_iga does_livestock business_profit business_employee total_employement conso_tot_pc conso_food_pc conso_nofood_pc z_assets_total value_n_iga {
					gen `var'_lkling = `var'
					gen `var'_ukling = `var'
					sum `var' if trt_cash == 0
					replace `var'_lkling = `r(mean)'+(0.25*`r(sd)') if trt_cash == 0 & attrition == 1		
					replace `var'_ukling = `r(mean)'-(0.25*`r(sd)') if trt_cash == 0 & attrition == 1
					sum `var' if trt_cash == 1
					replace `var'_lkling = `r(mean)'-(0.25*`r(sd)') if trt_cash == 1 & attrition == 1	
					replace `var'_ukling = `r(mean)'+(0.25*`r(sd)') if trt_cash == 1 & attrition == 1	
					pdslasso `var'_lkling trt_cash ($balance $X_poly_and_int i.a1_enumerator `var'_bl tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_lkling25) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_lkling_n) value(" `n'") format(%-9.0fc)
					pdslasso `var'_ukling trt_cash ($balance $X_poly_and_int i.a1_enumerator `var'_bl tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t_ukling25) all
					local n = `e(N)'
					insert_into_file using "sample_table.csv", key(`var'_t_ukling_n) value(" `n'") format(%-9.0fc)
					}
					
			table_from_tpl, t("TPL_tabA5.tex") r("sample_table.csv") o("tabA5.tex")
					
			
	

* TABLES A6
* --------------
			

							
	use "${data}/data_women.dta" , clear
	do "${data}/Lasso_var.do"	
	do "${tables}/stata-tex.do"

		
		
		
		*** Cost benefit analysis 
		
		** Benefits
		foreach var in conso_tot conso_perpetuity value_assets assetsANDanimals benefit_tot {
					pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) ,  partial(i.a1_enumerator tclp)  robust	
					mat A = r(table)
					local lbound = A[5,1]
					local ubound = A[6,1]
					di `lbound'
					di `ubound'
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_cba) all format(%-9.0fc)
					insert_into_file using "sample_table.csv", key(`var'_cba_lbound) value(" `lbound'") format(%-9.0fc)
					insert_into_file using "sample_table.csv", key(`var'_cba_ubound) value(" `ubound'") format(%-9.0fc)
		}
		
		foreach var in value_n_iga { 
					pdslasso `var' trt_cash ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) ,  partial(`var'_bl i.a1_enumerator tclp)  robust	
					mat A = r(table)
					local lbound = A[5,1]
					local ubound = A[6,1]
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_cba) all format(%-9.0fc)
					insert_into_file using "sample_table.csv", key(`var'_cba_lbound) value(" `lbound'") format(%-9.0fc)
					insert_into_file using "sample_table.csv", key(`var'_cba_ubound) value(" `ubound'") format(%-9.0fc)
		}
		
		** Benefits-cost ratios
		foreach var in costbenefittotal costbenefittotaldisc7 costbenefittotaldisc10  {
					pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) ,  partial(i.a1_enumerator tclp)  robust	
					mat A = r(table)
					local lbound = A[5,1]
					local ubound = A[6,1]
					di `lbound'
					di `ubound'
					store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_cba) all format(%-9.1fc)
					insert_into_file using "sample_table.csv", key(`var'_cba_lbound) value(" `lbound'") format(%-9.1fc)
					insert_into_file using "sample_table.csv", key(`var'_cba_ubound) value(" `ubound'") format(%-9.1fc)
		}
		
		
		** ITT effect on total post-treatment consumption
		pdslasso conso_tot trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) ,  partial(i.a1_enumerator tclp)  robust	
		local temp1 = -(_b[trt_cash]/ln(1-0.05))
		mat A = r(table)
		local lbound = A[5,1]
		local ubound = A[6,1]
		local temp2 = -`lbound'/ln(1-0.05)
		local temp3 = -`ubound'/ln(1-0.05)
		insert_into_file using "sample_table.csv", key(conso_onward_cba_beta) value(" `temp1'") format(%-9.0fc)
		insert_into_file using "sample_table.csv", key(conso_onward_cba_lbound) value(" `temp2'") format(%-9.0fc)
		insert_into_file using "sample_table.csv", key(conso_onward_cba_ubound) value(" `temp3'") format(%-9.0fc)
		
		** Number of years, such that total benefit = total cost
		pdslasso conso_tot trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) ,  partial(i.a1_enumerator tclp)  robust	
		local conso = _b[trt_cash]
		pdslasso assetsANDanimals trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) ,  partial(i.a1_enumerator tclp)  robust	
		local asset = _b[trt_cash]
		local nyears = (1130-`asset')/`conso'
		insert_into_file using "sample_table.csv", key(nyearsbenefcost) value(" `nyears'") format(%-9.1fc)

	
	table_from_tpl, t("TPL_tabA6.tex") r("sample_table.csv") o("tabA6.tex")

		


* TABLES A7-A11
* --------------
		
		 		
		** Women sample
		
		use "${data}/data_women.dta" , clear
		drop if attrition == 1
		do "${data}/Lasso_var.do"	
		
				* Analysis not ANCOVA   
				foreach var in 		c1_job_iga_1 c1_wageprimjob c1_hourswage c1_selfemployed business_profit c1_hoursself total_income total_employement c1_headincome30 c1_incomeotheriga  ///
									b2_assetnum19_sheep_iga b2_assetnum19_duck_iga b2_assetnum19_cows_iga b2_assetnum19_mule_iga b2_assetnum_sheep_iga b2_assetnum_duck_iga b2_assetnum_cows_iga b2_assetnum_mule_iga ///
									c2_borrow_all c2_repaiddebt c2_depositac c2_depositacn ///
									x9_1 x9_2 x9_3 x9_4 x9_5 x9_6 x9_7 x9_largepurchase x9_dailypurchase x9_wifepersonal ///
									x9_borrow x9_lend x9_occupation x9_workplace x9_workhours x9_participation  ///
									b4_sufferevent_10 b4_sufferevent_99 ///
									d1_nervousperson d1_heartedblue d1_cheerup d1_happyperson_transformed d1_calmpeaceful_transformed /// 
									b2_assetnum_room  b2_assetnum_mattresses b2_assetnum_radio b2_assetnum_cellphone b2_assetnum_smartphone b2_assetnum_refrigerator b2_assetnum_bicycles b2_assetnum_moto b2_assetnum_chaise b2_assetnum_tablette b2_assetnum_vent b2_assetnum_clim b2_assetnum_gr b2_assetnum_nat b2_assetnum_poch b2_assetnum_table b2_assetnum_salon b2_assetnum_bibli b2_assetnum_arm b2_assetnum_ferer b2_assetnum_mach b2_assetnum_dec /// 
									c4_hhmigreason_1 c4_hhmigreason_2 c4_hhmigreason_3 c4_hhmigreason_4 c4_hhmigreason_5 c4_hhmigreason_6 c4_hhmigreason_99 {
				sum `var' if trt_cash==0
				local sd = `r(sd)'
				insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
				insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
				pdslasso `var' trt_cash ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
				store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t) all
				pdslasso `var' trt_cash_0 trt_cash_1 ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
				store_est_tpl using "sample_table.csv" , coef(trt_cash_0) name(`var'_t0) all
				store_est_tpl using "sample_table.csv" , coef(trt_cash_1) name(`var'_t1) all
				local n = `e(N)'
				insert_into_file using "sample_table.csv", key(`var'_t1_n) value(" `n'") format(%-9.0fc)
				test trt_cash_0 = trt_cash_1
				insert_into_file using "sample_table.csv", key(`var'_test) value(" `r(p)'") format(%6.3f)
				}
					
				* Analysis ANCOVA
				foreach var in 		c2_borrow12 c2_borrow12n c2_eliipsav c2_eliipsavn c2_loan ///
									b4_business b4_sufferevent_6 b4_sufferevent_8 ///
									b4_death b4_disease b4_sufferevent_5 b4_sufferevent_9  ///
									b4_facesufferevent_1 b4_facesufferevent_2 b4_facesufferevent_7 ///
									b4_debts b4_facesufferevent_6 b4_help b4_sales b4_facesufferevent_15 {
				sum `var' if trt_cash==0
				local sd = `r(sd)'
				insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
				insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
				pdslasso `var' trt_cash ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
				store_est_tpl using "sample_table.csv" , coef(trt_cash) name(`var'_t) all
				pdslasso `var' trt_cash_0 trt_cash_1 ($balance $X_poly_and_int `var'_bl i.a1_enumerator tclp) , partial(i.a1_enumerator `var'_bl tclp) robust
				store_est_tpl using "sample_table.csv" , coef(trt_cash_0) name(`var'_t0) all
				store_est_tpl using "sample_table.csv" , coef(trt_cash_1) name(`var'_t1) all
				local n = `e(N)'
				insert_into_file using "sample_table.csv", key(`var'_t1_n) value(" `n'") format(%-9.0fc)
				test trt_cash_0 = trt_cash_1
				insert_into_file using "sample_table.csv", key(`var'_test) value(" `r(p)'") format(%6.3f)
				}

				
				
				
		** Partner sample
		
			preserve
			use "${data}/data_partners.dta" , clear
			do "${data}/Lasso_var.do"
			foreach var in z_gender_role z_abilities z_views z_reproductive z_bargaining z_communication {
			sum `var' if trt_cash_part_1==0
			local sd = `r(sd)'
			insert_into_file using "sample_table.csv", key(`var'_cmean) value(" `r(mean)'") format(%6.3f)
			insert_into_file using "sample_table.csv", key(`var'_csd) value(" `sd'") format(%6.3f)
			pdslasso `var' trt_cash_part_1 ($balance $X_poly_and_int i.a1_enumerator tclp) , partial(i.a1_enumerator tclp) robust
			mat A = r(table)
			local p_dialogue = A[4,1]
			di `p_dialogue'
			store_est_tpl using "sample_table.csv" , coef(trt_cash_part_1) name(`var'_t1) all
			}
			restore
		
		
		table_from_tpl, t("TPL_tabA7.tex") r("sample_table.csv") o("tabA7.tex")	
		table_from_tpl, t("TPL_tabA8.tex") r("sample_table.csv") o("tabA8.tex")
		table_from_tpl, t("TPL_tabA9.tex") r("sample_table.csv") o("tabA9.tex")
		table_from_tpl, t("TPL_tabA10.tex") r("sample_table.csv") o("tabA10.tex")
		table_from_tpl, t("TPL_tabA11.tex") r("sample_table.csv") o("tabA11.tex")			
		

	
		
		



